package gee

import (
	"fmt"
	"net/http"
)

// HandlerFunc defines the request handler used by gee
type HandlerFunc func(c *Context)

// Engine is the uni handler for all requests
type Engine struct {
	router *Router
}

// Engine 构造函数
func New() *Engine {
	return &Engine{
		newRouter(),
	}
}

func (engine *Engine) addRoute(method, path string, handler HandlerFunc) {
	engine.router.addRoute(method, path, handler)
}
func (engine *Engine) GET(relativePath string, handlerFunc HandlerFunc) {
	engine.addRoute(http.MethodGet, relativePath, handlerFunc)
}

func (engine *Engine) POST(relativePath string, handlerFunc HandlerFunc) {
	engine.addRoute(http.MethodPost, relativePath, handlerFunc)
}

func (engine *Engine) PUT(relativePath string, handlerFunc HandlerFunc) {
	engine.addRoute(http.MethodPut, relativePath, handlerFunc)
}
func (engine *Engine) DELETE(relativePath string, handlerFunc HandlerFunc) {
	engine.addRoute(http.MethodDelete, relativePath, handlerFunc)
}
func (engine *Engine) Run(addr string) (err error) {
	fmt.Sprintf("[GIN-debug] Listening and serving HTTP on %s\n", addr)
	err = http.ListenAndServe(addr, engine)
	return
}

func assert(guard bool, text string) {
	if !guard {
		panic(text)
	}
}

// 实现ServeHTTP方法后，拦截了所有的http请求，拥有了统一的控制入口
// 在这里一般实现自定义路由以及统一添加一些逻辑处理：如日志，异常处理等
func (engine *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	c := newContext(w, req)
	engine.router.handle(c)
}
